/*
 *	Load Monitor
 *
 *	Copyright © 2001 Alexandre Vial, some parts from Bernhard Baehr
 *
 *	LoadInfo.m - System load Container Class
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#import "LoadInfo.h"

#import "sys/sysctl.h"

@implementation MemInfo

- (id)initWithCapacity:(unsigned)numItems
{        
	self = [super init];
	size = numItems;
	vmdata = calloc(numItems, sizeof(VMData));
	if (vmdata == NULL) {
		NSLog (@"Failed to allocate buffer for MemInfo");
		return (nil);
	}
	inptr = 0;
	outptr = -1;
        
	return (self);
}


- (void)refresh
{        
        double loads[3];

        getloadavg(loads, 3);
        
        vmdata[inptr].load0 = loads[0];
        vmdata[inptr].load1 = loads[1];
        vmdata[inptr].load2 = loads[2];

	if (++inptr >= size)
		inptr = 0;
}


- (void)startIterate
{
	outptr = inptr;
}


- (BOOL)getNext:(VMDataPtr)ptr
{
	if (outptr == -1)
		return (FALSE);
	*ptr = vmdata[outptr++];
	if (outptr >= size)
		outptr = 0;
	if (outptr == inptr)
		outptr = -1;
	return (TRUE);
}


- (void)getCurrent:(VMDataPtr)ptr
{
	*ptr = vmdata[inptr ? inptr - 1 : size - 1];
}


- (void)getLast:(VMDataPtr)ptr
{
	*ptr = vmdata[inptr > 1 ? inptr - 2 : size + inptr - 2];
}


- (int)getSize
{
	return (size);
}


@end
